﻿using AutoMapper;
using Harmontronics.DataAccess.IOperate.Business;
using Harmontronics.DataAccess.Operate.Business;
using Harmontronics.IService;
using Harmontronics.IService.Business;
using Harmontronics.Model.Dto;
using Harmontronics.Model.Dto.Model;
using Harmontronics.Model.Entitys;
using Harmontronics.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Harmontronics.Service
{
    public class MaterialService : IMaterialService
    {
        private readonly IMapper _mapper;
        private readonly IUnitOfWork _unitOfWork;
        private IMaterialOperate _materialOperate;
        private IProcessOperate _processOperate;
        public MaterialService(IUnitOfWork unitOfWork, IMapper mapper, IMaterialOperate materialOperate, IProcessOperate processOperate)
        {
            _mapper = mapper;
            _unitOfWork = unitOfWork;
            _materialOperate = materialOperate;
            _processOperate = processOperate;
        }

        public MaterialModel Mending(Material entity)
        {
            MaterialModel model = _mapper.Map<MaterialModel>(entity);
            if (entity.ProcessId != null)
            {
                model.Process = _processOperate.GetProcessById(entity.ProcessId);
            }
            else
            {
                model.Process = new Process()
                {
                    ProcessId = entity.ProcessId
                };
            }
            return model;
        }

        public List<MaterialModel> MendingRange(List<Material> entities)
        {
            List<MaterialModel> models = new List<MaterialModel>();
            foreach (var entity in entities)
            {
                models.Add(Mending(entity));
            }
            return models;
        }
        public List<Material> GetAllMaterials()
        {
            try
            {
                return _materialOperate.GetAllMaterialList();
                //return _db.Materials.ToList();
            }
            catch (Exception ex)
            {
                return new List<Material>();
            }
        }

        public Material GetMaterialById(string id)
        {
            try
            {
                return _materialOperate.GetMaterialById(id);
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public bool AddMaterial(MaterialEdit edit)
        {
            try
            {
                Material entity = _mapper.Map<Material>(edit);
                _materialOperate.AddMaterial(entity);
                _unitOfWork.Commit();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool UpdateMaterial(MaterialEdit model)
        {
            try
            {
                Material entity = _materialOperate.GetMaterialById(model.MaterialId);
                _mapper.Map(model, entity);
                _materialOperate.UpdateMaterial(entity);
                _unitOfWork.Commit();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool DeleteMaterial(string id)
        {
            try
            {
                Material entity = _materialOperate.GetMaterialById(id);
                if (entity != null)
                {
                    _materialOperate.DeleteMaterial(entity);
                    _unitOfWork.Commit();
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool BatchDelMaterial(List<string> ids)
        {
            try
            {
                List<Material> entities = _materialOperate.GetMaterialByIdList(ids);
                if (entities.Count > 0)
                {
                    _materialOperate.BatchDelMaterial(entities);
                    _unitOfWork.Commit();
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }

        }
    }
}
